####################################################################
## author:    Robert A. Huber
## contact:   robert.huber@ir.gess.ethz.ch
## file name: pc_uk_export.R
## Context:   Populism and Climate Sceptism, individuals from BES
## started:   2016-10-12
## Summary:   Exports Results
######################################################################

# Plot Output -------------------------------------------------------------

list_p <- c("pcc1", "pcc2", "pcc2_RBC","peG1", "peG2", "peG2_RBC", "peP1", "peP2", "peP2_RBC", "peP3", "peP3_RBC", "p_eG2_margins", "p_eG2_RBC_margins", "pcc2_res", "peG2_res", "p_eG2_res_margins", "peP3_res", "pcc1_ord", "peP1_ord", "pcc2_ord", "peP3_ord", "p_pred")
#, "p_lrpop", "p_bootCC1", "p_bootCC2", "p_bootEG1", "p_bootEG2", "p_bootEP1", "p_bootEP2", "p_lrscales", "p_dvs"

for (i in list_p){
  pdf(paste0("./figs/PC_", i,".pdf",sep=""), width = 11.69, height = 8.27)
  a <- get(i)
  plot(a)
  dev.off()
}

for (i in list_p){
  png(paste0("./figs/", i,".png",sep=""), width = 900, height = 600)
  a <- get(i)
  plot(a)
  dev.off()
}

# pdf("Summary.pdf", width = 25, height = 15)
# pcc1
# pcc2
# peP1
# peP2
# peP3
# peG1
# peG2
# pcc2_RBC
# peP2_RBC
# peP3_RBC
# peG2_RBC
# p_eG2_margins
# p_eG2_RBC_margins
# pcc2_res
# peG2_res
# p_eG2_res_margins
# peP3_res
# pcc2_ord
# peP3_ord
# dev.off()

fa_out_pop

myvars <- c("climateChange", "climateChange_ord", "enviroGrowth", "enviroProtection", "enviroProtection_RBC", "enviroProtection_ord", "populism", "lr", "lr_scale", "auth1", "auth2", "auth3", "auth4", "gender", "inc", "age" , "edu_high", "riskTaking", "polAttention", "satDem", "efficacy", "genTrust", "econPersonalRetro",  "econGenRetro" , "partyId_ukip", "partyId_con", "partyId_lab", "partyId_lib", "partyId_gre", "partyId_oth")

df_ds <- df_bes[,myvars]
# 
# zzz <- Hmisc::describe(x=df_ds)
# xxx <- latex(zzz, file = "./descriptives.tex")
# text2 <-"\\documentclass{article}\n\\usepackage{relsize,setspace}\n\\begin{document}\n\\textbf{\\huge
# Descriptive Statistics}\n\n\\textbf{\\large Experiment 1 -- Germany -- no weights applied}\n\n\\input{descriptives} \n\\end{document}"
# cat(text2, file = "./descriptives.tex11")
# require(tools)
# texi2dvi("./descriptives.tex", pdf = TRUE)

df_ds$climateChangeBelieve <- ifelse(df_ds$climateChange == "Climate changing due to human activity", 1, 0)
df_ds$climateChangeScepticism <- ifelse(df_ds$climateChange == "Climate changing but not due to human activity", 1, 0)
df_ds$climateChangeDenial <- ifelse(df_ds$climateChange == "Climate not changing", 1, 0)
df_ds$climateChangeDK <- ifelse(df_ds$climateChange == "Don't know", 1, 0)
df_ds$enviroProtectionNE <- ifelse(df_ds$enviroProtection_RBC == "Not enough", 1, 0)
df_ds$enviroProtectionAR <- ifelse(df_ds$enviroProtection_RBC == "About right", 1, 0)
df_ds$enviroProtectionGTF <- ifelse(df_ds$enviroProtection_RBC == "Gone too far", 1, 0)
df_ds$enviroProtectionDK <- ifelse(df_ds$enviroProtection_RBC == "Don't know", 1, 0)
df_ds$auth1RE <- ifelse(df_ds$auth1 == "Respect for elders", 1, 0)
df_ds$auth1IN <- ifelse(df_ds$auth1 == "Independence", 1, 0)
df_ds$auth1DK <- ifelse(df_ds$auth1 == "Don't know", 1, 0)
df_ds$auth2OB <- ifelse(df_ds$auth2 == "Obedience", 1, 0)
df_ds$auth2SR <- ifelse(df_ds$auth2 == "Self-reliance", 1, 0)
df_ds$auth2DK <- ifelse(df_ds$auth2 == "Don't know", 1, 0)
df_ds$auth3WB <- ifelse(df_ds$auth3 == "To be well-behaved", 1, 0)
df_ds$auth3CO <- ifelse(df_ds$auth3 == "To be considerate", 1, 0)
df_ds$auth3DK <- ifelse(df_ds$auth3 == "Don't know", 1, 0)
df_ds$auth4GM <- ifelse(df_ds$auth4 == "Good manners", 1, 0)
df_ds$auth4CU <- ifelse(df_ds$auth4 == "Curiosity", 1, 0)
df_ds$auth4DK <- ifelse(df_ds$auth4 == "Don't know", 1, 0)
df_ds$edu_uni <- ifelse(df_ds$edu_high == "University degree",1, 0)
df_ds$riskTakingVU <- ifelse(df_ds$riskTaking == "Very unwilling to take risks", 1, 0)
df_ds$riskTakingSU <- ifelse(df_ds$riskTaking == "Somewhat unwilling to take risks", 1, 0)
df_ds$riskTakingSW <- ifelse(df_ds$riskTaking == "Somewhat willing to take risks", 1, 0)
df_ds$riskTakingVW <- ifelse(df_ds$riskTaking == "Very willing to take risks", 1, 0)
df_ds$genTrustTR <- ifelse(df_ds$genTrust == "Most people can be trusted", 1, 0)
df_ds$genTrustNT <- ifelse(df_ds$genTrust == levels(df_bes$genTrust)[2], 1, 0)
df_ds$genTrustDK <- ifelse(df_ds$genTrust == levels(df_bes$genTrust)[3],1, 0)
df_ds$econPersonalRetroALT <- ifelse(df_ds$econPersonalRetro == "Got a lot worse", 1, 0)
df_ds$econPersonalRetroLIW <- ifelse(df_ds$econPersonalRetro == "Got a little worse", 1, 0)
df_ds$econPersonalRetroSAM <- ifelse(df_ds$econPersonalRetro == "Stayed the same", 1, 0)
df_ds$econPersonalRetroLIB <- ifelse(df_ds$econPersonalRetro == "Got a little better", 1, 0)
df_ds$econPersonalRetroALB <- ifelse(df_ds$econPersonalRetro == "Got a lot better", 1, 0)
df_ds$econPersonalRetroDKN <- ifelse(df_ds$econPersonalRetro == "Don't know", 1, 0)
df_ds$econGenRetroALT <- ifelse(df_ds$econGenRetro == "Got a lot worse", 1, 0)
df_ds$econGenRetroLIW <- ifelse(df_ds$econGenRetro == "Got a little worse", 1, 0)
df_ds$econGenRetroSAM <- ifelse(df_ds$econGenRetro == "Stayed the same", 1, 0)
df_ds$econGenRetroLIB <- ifelse(df_ds$econGenRetro == "Got a little better", 1, 0)
df_ds$econGenRetroALB <- ifelse(df_ds$econGenRetro == "Got a lot better", 1, 0)
df_ds$econGenRetroDKN <- ifelse(df_ds$econGenRetro == "Don't know", 1, 0)
df_ds$satDemVD <- ifelse(df_ds$satDem == "Very dissatisfied", 1, 0)
df_ds$satDemLD <- ifelse(df_ds$satDem == "A little dissatisfied", 1, 0)
df_ds$satDemFS <- ifelse(df_ds$satDem == "Fairly satisfied", 1, 0)
df_ds$satDemVS <- ifelse(df_ds$satDem == "Very satisfied", 1, 0)
df_ds$satDemDK <- ifelse(df_ds$satDem == "Don't know", 1, 0)
library(stargazer)
stargazer(df_ds, type = "html", file = "./tables/appendix/Descriptives.html")



# Regression Tables -------------------------------------------------------

htmlreg(mcc.1,
        file = "./tables/Table1.html",
        custom.model.names = c("Scepticism", "Denial", "Don't know"),
        leading.zero = T,
        digits = 2,
        custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                                     "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                                     "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                                     "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                                     "Party Identification - Gre", "Party Identification - Other"),
        reorder.coef = c(2:3, 4:22,1),
        stars = c(0.1,0.05,0.01),caption = "Regression Table: Climate Scepticism", 
        caption.above = T,
        label = "Populism and Climate Scepticism",
        longtable = T,
        single.row = T)

texreg(mcc.1,
        custom.model.names = c("Scepticism", "Denial", "Don't know"),
        leading.zero = T,
        digits = 2,
        custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                              "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                              "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                              "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                              "Party Identification - Gre", "Party Identification - Other"),
        reorder.coef = c(2:3, 4:22,1),
        stars = c(0.1,0.05,0.01),caption = "Regression Table: Climate Scepticism", 
        caption.above = T,
        label = "tcc1FULL",
        longtable = T,
        single.row = F, custom.note = "%stars")


texreg(mcc.2,
       custom.model.names = c("Scepticism", "Denial", "Don't know"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
       reorder.coef = c(2:3,23, 4:22,1),
       stars = c(0.1,0.05,0.01),
       caption = "Regression Table: Climate Scepticism",
       caption.above = T,
       label = "RegTab:Climate",
       longtable = T,
       single.row = T)


htmlreg(list(meP.1, meG.1),
        file= "./tables/Table2.html",
       custom.model.names = c("Don't know", "Gone too far", "Not gone far enough", "Environmental Protection vs. Economic Growth"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other"),
       reorder.coef = c(2:3, 4:22,1),
       stars = c(0.1,0.05,0.01),
       caption = "Populism and Support for Environmental Policy",
       caption.above = T,
       label = "Populism and Support for Environmental Policy",
       longtable = T,
       single.row = T)

texreg(list(meP.1, meG.1),
        #file= "./tables/Table2.html",
        custom.model.names = c("Don't know", "Gone too far", "Not gone far enough", "Environmental Protection vs. Economic Growth"),
        leading.zero = T,
        digits = 2,
        custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                              "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                              "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                              "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                              "Party Identification - Gre", "Party Identification - Other"),
        reorder.coef = c(2:3, 4:22,1),
        stars = c(0.1,0.05,0.01),
        caption = "Populism and Support for Environmental Policy",
        caption.above = T,
        label = "tepeg1FULL",
        longtable = T, custom.note = "%stars",
        single.row = F)

texreg(list(meP.3, meG.2),
       custom.model.names = c("Don't know", "Gone too far", "Not gone far enough", "Environmental Protection vs. Economic Growth"),
       leading.zero = T,
        digits = 2,
        custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                              "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                              "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                              "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                              "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
        reorder.coef = c(2:3,23, 4:22,1),
        stars = c(0.1,0.05,0.01),
        caption = "Regression Table: Support for Environmental Protection",
        caption.above = T,
        label = "tepeg1",
        longtable = T, custom.note = "%stars",
        single.row = F)


texreg(mcc.2_lr,
       custom.model.names = c("Scepticism", "Denial", "Don't know"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
       reorder.coef = c(2:3,23, 4:22,1),
       stars = c(0.1,0.05,0.01),
       caption = "Populism, Political Ideology, and Climate Scepticism",
       caption.above = T,
       label = "RegTab:Climate_lr",
       longtable = T,
       single.row = T)

texreg(list(meP.3_lr, meG.2_lr),
       custom.model.names = c("Don't know", "Gone too far", "Not gone far enough", "Environmental Protection vs. Economic Growth"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
       reorder.coef = c(2:3,23, 4:22,1),
       stars = c(0.1,0.05,0.01),
       caption = "Populism, Political Ideology, and Support for Environmental Protection",
       caption.above = T,
       label = "RegTab:EnvPro_res",
       longtable = T,
       single.row = T)

texreg(mcc.2_res,
       custom.model.names = c("Scepticism", "Denial", "Don't know"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
       reorder.coef = c(2:3,23, 4:22,1),
       stars = c(0.1,0.05,0.01),
       caption = "Populism, Political Ideology, and Climate Scepticism",
       caption.above = T,
       label = "RegTab:Climate_lr",
       longtable = T,
       single.row = T)

texreg(list(meP.3_res, meG.2_res),
       custom.model.names = c("Don't know", "Gone too far", "Not gone far enough", "Environmental Protection vs. Economic Growth"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
       reorder.coef = c(2:3,23, 4:22,1),
       stars = c(0.1,0.05,0.01),
       caption = "Populism, Political Ideology, and Support for Environmental Protection",
       caption.above = T,
       label = "RegTab:EnvPro_res",
       longtable = T,
       single.row = T)

texreg(list(mcc.1_ord, meP.3_ord),
       custom.model.names = c("Climate Scepticism", "Environmental Protection"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c("Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other"),
       #reorder.coef = c(2:3, 4:29,1),
       stars = c(0.1,0.05,0.01),
       caption = "Populism", 
       caption.above = T,
       label = "RBC_ord",
       longtable = T,
       single.row = T)

texreg(list(mcc.2.ord, meP.4_ord),
       custom.model.names = c("Climate Scepticism", "Environmental Protection"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c("Populism", "Political Ideology", "Authoritarianism", "Gender (Female = 1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
       reorder.coef = c(1:2,22,3:21),
       stars = c(0.1,0.05,0.01),
       caption = "Populism and Political Ideology", 
       caption.above = T,
       label = "RBC_ord_int",
       longtable = T,
       single.row = T)

texreg(list(mcc1_logit, mcc2_logit),
       custom.model.names = c("Baseline", "Interaction"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
       reorder.coef = c(2:3, 23, 4:22,1),
       stars = c(0.1,0.05,0.01),caption = "Regression Table: Climate Scepticism using Logit", 
       caption.above = T,
       label = "tcclogit",
       longtable = T,
       single.row = F, custom.note = "%stars")

htmlreg(list(mcc1_logit, mcc2_logit),
        file = "../logit.html",
       custom.model.names = c("Baseline", "Interaction"),
       leading.zero = T,
       digits = 2,
       custom.coef.names = c(NA, "Populism", "Political Ideology", "Authoritarianism", "Gender (Female =1)", "Income", "Age",
                             "Education - University", "Risk Taking", "Political Attention", "Satisfaction w/ Democracy", "Efficacy", "Societal Trust - Can't be to careful",
                             "Societal Trust - DK", "Retro. Per. Eco. Dev.", "Retro. Soc. Eco. Dev.","Party Identification - UKIP", 
                             "Party Identification - Con", "Party Identification - Lab", "Party Identification - Lib Dems", 
                             "Party Identification - Gre", "Party Identification - Other", "Populism x Political Ideology"),
       reorder.coef = c(2:3, 23, 4:22,1),
       stars = c(0.1,0.05,0.01),caption = "Regression Table: Climate Scepticism using Logit", 
       caption.above = T,
       label = "tcclogit",
       longtable = T,
       single.row = T, custom.note = "%stars")

rm(list=setdiff(ls(), c("df_bes")))
